// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db=cloud.database();
const _ = db.command;

// 云函数入口函数
exports.main = async (event, context) => {
  switch (event.action) {
    case 'patient': {
      return patient(event)
    }
    case 'ao': {
      return ao(event)
    }
    case 'ss': {
      return ss(event)
    }
    case 'bo_adult': {
      return bo_adult(event)
    }
    case 'bo_child': {
      return bo_child(event)
    }
    case 'ward': {
      return ward(event)
    }
    case 'sche': {
      return sche(event)
    }
    default: {
      return
    }
  }
}
 
async function patient(event) {
  var{patient_name,patient_bed,patient_sex,patient_age,patient_blood_type,patient_chief,patient_diag,patient_treat,patient_oper_list_name,patient_oper_list_time,patient_others,patient_otherchief}=event
  patient_oper_list_time.time=new Date(patient_oper_list_time.time)
  for(var i=0;i<patient_others.length;i++){
    patient_others[i].time=new Date(patient_others[i].time)
  }
  //将手术类型手术日期进行组合
  var patient_oper_list=[
    {'name':patient_oper_list_name,'time':new Date(patient_oper_list_time)}
  ]
  // patient_oper_list.push.apply(patient_oper_list, patient_others);
  patient_oper_list=[...patient_oper_list,...patient_others]
  //将主管医师进行组合
  var patient_chief=[
    patient_chief
  ]
  // patient_chief.push.apply(patient_chief, patient_otherchief);
  patient_chief=[...patient_chief,...patient_otherchief]
  return await db.collection('patient').add({
    data:{
      patient_name:patient_name,
      patient_bed:patient_bed,
      patient_sex:patient_sex,
      patient_age:patient_age,
      patient_blood_type:patient_blood_type,
      patient_chief:patient_chief,
      patient_diag:patient_diag,
      patient_treat:patient_treat,
      patient_in_time:new Date(),
      patient_oper_list:patient_oper_list,
      patient_del:false
    }
  })
}

async function ao(event) {
  var {patient_id,ao_WBC,ao_HB,ao_PLT,ao_TB,ao_ALT,ao_AST,ao_GGT,ao_ALB,ao_ALP,ao_FK浓度,ao_FK,ao_激素,ao_T管,ao_体温,ao_入量,ao_出量,ao_尿量,ao_desc,ao_others}=event
  var ao_index=[
    {'name':'WBC','value':ao_WBC},
    {'name':'HB','value':ao_HB},
    {'name':'PLT','value':ao_PLT},
    {'name':'TB','value':ao_TB},
    {'name':'ALT','value':ao_ALT},
    {'name':'AST','value':ao_AST},
    {'name':'GGT','value':ao_GGT},
    {'name':'ALB','value':ao_ALB,},
    {'name':'ALP','value':ao_ALP},
    {'name':'FK浓度','value':ao_FK浓度},
    {'name':'FK','value':ao_FK},
    {'name':'激素','value':ao_激素},
    {'name':'T管','value':ao_T管},
    {'name':'体温','value':ao_体温},
    {'name':'入量','value':ao_入量},
    {'name':'出量','value':ao_出量},
    {'name':'尿量','value':ao_尿量}
  ]
  //  ao_index.push.apply(ao_index, ao_others);
  ao_index=[...ao_index,...ao_others]
  return await db.collection('ao').add({
    data:{
        patient_id:patient_id,
        ao_index:ao_index,
        ao_desc:ao_desc,
        ao_time:new Date()
    }
  })
}

async function ss(event) {
  var {patient_id,ss_temper_top,ss_temper_down,ss_rate_top,ss_rate_down,ss_press_top,ss_press_down,ss_blood_oxygen,ss_mind,ss_vision,ss_touch,ss_knock,ss_hear,ss_desc,ss_examine,ss_yester_desc,ss_openid}=event
  return await db.collection('ss').add({
    data:{
         _openid:ss_openid,
        patient_id:patient_id,
        ss_time:Date(),
        ss_feature:{
          'temper_top':ss_temper_top,
          'temper_down':ss_temper_down,
          'rate_top':ss_rate_top,
          'rate_down':ss_rate_down,
          'press_top':ss_press_top,
          'press_down':ss_press_down,
          'blood_oxygen':ss_blood_oxygen,
          'mind':ss_mind,
          'belly_feat':{
            'vision':ss_vision,
            'touch':ss_touch,
            'knock':ss_knock,
            'hear':ss_hear
          }
        },
        ss_desc:ss_desc,
        ss_examine:ss_examine,
        ss_yester_desc:ss_yester_desc
    }
  })
}

async function bo_adult(event) {
  var {bo_name,bo_bed,bo_sex,bo_age,bo_height,bo_weight,bo_blood_type,bo_situation,bo_meld,bo_child,bo_diag}=event
  return await db.collection('bo').add({
    data:{
        bo_age_type:"成人",
        bo_name:bo_name,
        bo_bed:bo_bed,
        bo_sex:bo_sex,
        bo_age:bo_age,
        bo_height:bo_height,
        bo_weight:bo_weight,
        bo_blood_type:bo_blood_type,
        bo_situation:bo_situation,
        bo_meld:bo_meld,
        bo_child:bo_child,
        bo_diag:bo_diag,
        bo_del:false
    }
  })
}

async function bo_child(event) {
  var {bo_name,bo_bed,bo_sex,bo_age,bo_height,bo_weight,bo_blood_type,bo_situation,bo_peld,bo_child,bo_diag}=event
  return await db.collection('bo').add({
    data:{
        bo_age_type:"儿童",
        bo_name:bo_name,
        bo_bed:bo_bed,
        bo_sex:bo_sex,
        bo_age:bo_age,
        bo_height:bo_height,
        bo_weight:bo_weight,
        bo_blood_type:bo_blood_type,
        bo_situation:bo_situation,
        bo_peld:bo_peld,
        bo_child:bo_child,
        bo_diag:bo_diag,
        bo_del:false
    }
  })
}

async function ward(event) {
  var {ward_turnbed,ward_turnbed_again,ward_add_bed,ward_oper,ward_bed_appoint,ward_others_hos,ward_bef_A,ward_bef_B,ward_bef_AB,ward_bef_O,ward_wait_A,ward_wait_B,ward_wait_AB,ward_wait_O,ward_time,ward_lastid}=event
  return await db.collection("ward").doc(ward_lastid).update({
    data:{
      ward_turnbed:ward_turnbed,
      ward_turnbed_again:ward_turnbed_again,
      ward_add_bed:ward_add_bed,
      ward_bef_asse:{
        'A':ward_bef_A,
        'B':ward_bef_B,
        'AB':ward_bef_AB,
        'O':ward_bef_O
      },
      ward_wait_oper:{
        'A':ward_wait_A,
        'B':ward_wait_B,
        'AB':ward_wait_AB,
        'O':ward_wait_O,
      },
      ward_in:[],
      ward_out:[],
      ward_oper:ward_oper,
      ward_bed_appoint:ward_bed_appoint,
      ward_others_hos:ward_others_hos,
      ward_time:new Date(ward_time),
      ward_submit:true,
      }
  }).then(res=>{
    db.collection('ward').add({
      data:{
          ward_turnbed:"",
          ward_turnbed_again:"",
          ward_add_bed:"",
          ward_bef_asse:{
            'A':"",
            'B':"",
            'AB':"",
            'O':"",
          },
          ward_wait_oper:{
            'A':"",
            'B':"",
            'AB':"",
            'O':"",
          },
          ward_in:[],
          ward_out:[],
          ward_oper:[],
          ward_bed_appoint:"",
          ward_others_hos:"",
          ward_time:new Date(ward_time+86400000),
          ward_submit:false,
      }
    })
  })
}

async function sche(event) {
  var {sche_group,sche_chief,sche_deputy_chief,sche_attend_doc,sche_resi_doc,sche_rota_doc,sche_rest_time,sche_discuss_time,sche_pub_edu,sche_in_consul,sche_out_consul,sche_department,sche_desc,sche_month,sche_morechief,sche_moredeputy,sche_moreattend,sche_moreresi,sche_morerota,sche_day_list}=event
  //将主任医师进行组合
  var sche_chief=[
    sche_chief
  ]
  sche_chief=[...sche_chief,...sche_morechief]
  //将副主任医师进行组合
  var sche_deputy_chief=[
    sche_deputy_chief
  ]
  sche_deputy_chief=[...sche_deputy_chief,...sche_moredeputy]
  //将主治医师进行组合
  var sche_attend_doc=[
    sche_attend_doc
  ]
  sche_attend_doc=[...sche_attend_doc,...sche_moreattend]
  //将住院医师进行组合
  var sche_resi_doc=[
    sche_resi_doc
  ]
  sche_resi_doc=[...sche_resi_doc,...sche_moreresi]
  //将轮转医师进行组合
  var sche_rota_doc=[
    sche_rota_doc
  ]
  sche_rota_doc=[...sche_rota_doc,...sche_morerota]

  return await db.collection('sche').add({
    data:{
        sche_month:sche_month,
        sche_group:sche_group,
        sche_chief:sche_chief,
        sche_deputy_chief:sche_deputy_chief,
        sche_attend_doc:sche_attend_doc,
        sche_resi_doc:sche_resi_doc,
        sche_rota_doc:sche_rota_doc,
        sche_rest_time:sche_rest_time,
        sche_discuss_time:sche_discuss_time,
        sche_pub_edu:sche_pub_edu,
        sche_in_consul:sche_in_consul,
        sche_out_consul:sche_out_consul,
        sche_department:sche_department,
        sche_desc:sche_desc,
        sche_day_list:sche_day_list
    }
  })
}